package com.financing.wap.weixin.util;

import java.io.File;

import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class Test{

   public static final String GET_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";// 获取access
   public static final String UPLOAD_IMAGE_URL = "http://file.api.weixin.qq.com/cgi-bin/media/upload";// 上传多媒体文件
   public static final String APP_ID = "wxa549b28c24cf341e";
   public static final String SECRET = "78d8a8cd7a4fa700142d06b96bf44a37";

   /**
    * 上传多媒体文件
    * 
    * @param url
    *            访问url
    * @param access_token
    *            access_token
    * @param type
    *            文件类型
    * @param file
    *            文件对象
    * @return
    */
   public static String uploadImage(String url, String access_token,
           String type, File file)
   {
       org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
       String uploadurl = String.format("%s?access_token=%s&type=%s", url,
               access_token, type);
       PostMethod post = new PostMethod(uploadurl);
       post
               .setRequestHeader(
                       "User-Agent",
                       "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:30.0) Gecko/20100101 Firefox/30.0");
       post.setRequestHeader("Host", "file.api.weixin.qq.com");
       post.setRequestHeader("Connection", "Keep-Alive");
       post.setRequestHeader("Cache-Control", "no-cache");
       String result = null;
       try
       {
           if (file != null && file.exists())
           {
               FilePart filepart = new FilePart("media", file, "image/jpeg",
                       "UTF-8");
               Part[] parts = new Part[] { filepart };
               MultipartRequestEntity entity = new MultipartRequestEntity(
                       parts,

                       post.getParams());
               post.setRequestEntity(entity);
               int status = client.executeMethod(post);
               if (status == HttpStatus.SC_OK)
               {
                   String responseContent = post.getResponseBodyAsString();
                   System.out.println("responseContent:"+responseContent);
               }
           }
       }
       catch (Exception e)
       {
           e.printStackTrace();
       }
       finally
       {
           return result;
       }
   }

   public static void main(String[] args) throws Exception
   {
       String accessToken = "ZesjKigY5ozj0WHs5Rh2ss3r21QV144nKe-0aiHALRcIwmRv9reN3xrDe5sJy2PMcPYiUblTm7o60gV8Tm2h59xw3TKEskDnn2pJM9hz4h2Ob-qFR1gvpk-7ZakB3-_oMDPhAGAXOA";
       if (accessToken != null)// token成功获取
       {
           System.out.println(accessToken);
           File file = new File("d:" + File.separator + "b.jpg"); // 获取本地文件
           String id = uploadImage(UPLOAD_IMAGE_URL, accessToken, "image",
                   file);// 上传文件
           if (id != null)
               System.out.println(id);
       }
   }
}
